<?php
class Administrator_Model_System extends Nguyen_System{
	public function __construct(){
		$this->db = Zend_Registry::get('vicdbsystem');
		$this->lang = new Nguyen_language_system();
	}
	public function GetLanguage(){
		$sql = 'select * from language';
		return $this->db->fetchAll($sql);
	}	
	public function GetLanguageDefault(){
		$sql = 'select * from language where status = 1';
		return $this->db->fetchRow($sql);
	}
	public function GetLanguageDefaultId(){
		$sql = 'select id from language where status = 1';
		$result = $this->db->fetchRow($sql);
		return $result['id'];
	}
	public function UpdateLanguageDefault($id){
		$where = "status = 1";
		$data = array(
				'status'  => 0
				);
		$this->db->update('language', $data, $where);
		$where = "id = '".$id."'";
		$data = array(
				'status'  => 1
				);
		$this->db->update('language', $data, $where);
	}
	public function Translate($id){
		$default = $this->GetLanguageDefault();
		$sql = 'select '.$default['id'].' from language_content where id = "'.$id.'"';
		$result = $this->db->fetchRow($sql);
		return $result[$default['id']];
	}
	public function TranslateByLang($id,$lang){
		$sql = 'select '.$lang.' from language_content where id = "'.$id.'"';
		$result = $this->db->fetchRow($sql);
		if($result[$lang]==''){
			return $this->Translate($id);
		}
		return $result[$lang];
	}
	public function DeleteImageOnHost($directory,$image){
		unlink(PUBLIC_PATH.'/images/'.$directory.'/'.$image);
	}
	public function ImageUrl($dir,$url){
		if($url==''){return PUBLIC_URL.'/images/no_image.gif';}
		if ($this->checkUrl($url)){
			return $url;
		}
		return PUBLIC_URL.'/images/'.$dir.'/'.$url;
	}
	public function insert1rowInLang_content($lang,$content){
		if($lang == 'vi')
			$data = array(
					'vi'     => $content
			);
		else
			$data = array(
					'en'     => $content
			);
		$this->db->insert('language_content',$data);
		return $this->db->lastInsertId();
	}
	public function  InsertLangContent($vi,$en){
		$data = array(
			'vi'     => $vi,
			'en'     => $en		
		);
		$this->db->insert('language_content',$data);
		return $this->db->lastInsertId();
	}
	public function  UpdateLangContent($id,$vi,$en){
		$where = 'id='.$id;
		$data = array(
			'vi'     => $vi,
			'en'     => $en		
		);
		$this->db->update('language_content',$data,$where);
	}
	public function deleteLangContent($id){	
		$where = "id='".$id."'";
		$this->db->delete('language_content',$where);
	}
	public function GetSiteInfo(){
		$sql = 'select * from site_info';
		return $this->db->fetchRow($sql);
	}
	public function UpdateSiteInfoNoAdmin($sitename,$meta,$description,$email,$website,$fax,$phone,$mobile,$address,$status){
		$where = '';
		$data = array ('sitename' => $sitename, 
						'meta' => $meta, 
						'email' => $email, 
						'description' => $description, 
						'website' => $website, 
						'fax' => $fax,
						'phone' => $phone,
						'mobile' => $mobile,
						'address' => $address,
						'status' => $status );
		$this->db->update ( 'site_info', $data,$where );
	}
	public function UpdateSiteInfo($sitename,$meta,$description,$email,$website,$fax,$phone,$mobile,$address,$siteoffline,$offnotice,$status){
		$where = '';
		$data = array ('sitename' => $sitename, 
						'meta' => $meta, 
						'email' => $email, 
						'description' => $description, 
						'website' => $website, 
						'fax' => $fax,
						'phone' => $phone,
						'mobile' => $mobile,
						'address' => $address,
						'siteoffline' => $siteoffline,
						'offnotice' => $offnotice,
						'status' => $status );
		$this->db->update ( 'site_info', $data,$where );
	}
}